/*
 * This file is part of the eccOS Library.
 * Copyright (C) 2007.
 *
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 */
package org.openeccos.gui;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.Border;
import nextapp.echo2.app.Button;
import nextapp.echo2.app.Color;
import nextapp.echo2.app.Component;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.FillImage;
import nextapp.echo2.app.FillImageBorder;
import nextapp.echo2.app.Font;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.LayoutDirection;
import nextapp.echo2.app.Row;
import nextapp.echo2.app.SplitPane;

import org.openeccos.dao.EccosService;
import org.openeccos.model.MQuickle;
import org.openeccos.util.EccosDefaultLookAndFeel;
import org.openeccos.util.PDUtil;

import com.sas.framework.expojo.ModelExposer;

import echopointng.LabelEx;
import echopointng.SelectFieldEx;
import echopointng.TextAreaEx;
import echopointng.TextFieldEx;

/**
 * A post-it like yellow notice which you can place on the desktop
 *
 * @author cgspinner@web.de
 */
public class PDQuickle extends PDWindowPane {

	protected Row toolbar;
	private MQuickle model;
	private boolean loading = true;
	private TextAreaEx txtText;
	private TextFieldEx txtAuthor;

	public PDQuickle(MQuickle m) {
		this.model = m;
		initGUI();
	}


	private void initGUI() {
		setTitle(model.getTitle());
		init2();
		txtText.setText(model.getText());
		txtAuthor.setText(model.getAuthor());

		addPropertyChangeListener(new PropertyChangeListener() {
			public void propertyChange(PropertyChangeEvent evt) {
				updateModel();
				if (loading) return;
				// this event is called twice, one for X and one for Y when
				// the user moves window
				EccosService service =(EccosService)ModelExposer.get().getService(EccosService.NAME);
				service.persist(model);
			}
		});
	}

	public Button addToolButton(String text, String image) {
		return addToolButton(text, image, false);
	}

	public Button addToolButton(String text, String image, boolean showText) {
		Button btn = new Button(image);
		if (showText) {
			btn.setText(text);
		} else {
			btn.setToolTipText(text);
		}
		getToolbar().add(btn);
		return btn; // we return the btn, so it is easy to attach a listener
	}

	public Row getToolbar() {
		if (toolbar == null) {
			toolbar = new Row();
			toolbar.setCellSpacing(new Extent(0));
			toolbar.setInsets(new Insets(0, 0, 0, 0));
			toolbar.setLayoutDirection(LayoutDirection.RTL);
			toolbar.setAlignment(new Alignment(Alignment.RIGHT, Alignment.TOP));
			add(toolbar);
		}
		return toolbar;
	}

	private void init2() {

		// setBackground(Color.YELLOW);
		setBackgroundImage(new FillImage(PDUtil.getImage("img/semitrans4.png")));
		setPositionX(new Extent(model.getPositionX()));
		setPositionY(new Extent(model.getPositionY()));
		if (model.getWidth() > 0) {
			setWidth(new Extent(model.getWidth()));
		} else {
			setWidth(new Extent(250));
		}
		if (model.getHeight() > 0) {
			setHeight(new Extent(model.getHeight()));
		} else {
			setHeight(new Extent(180));
		}
		setInsets(new Insets(0, 0, 0, 0));
		setIconInsets(new Insets(7, 3, 0, 0));
		setBorder(new FillImageBorder(EccosDefaultLookAndFeel.HIGHLIGHT_COLOR, new Insets(4), new Insets(4)));

		setResizable(true);

		setTitleFont(new Font(Font.SANS_SERIF, Font.BOLD, new Extent(12)));
		setTitleBackgroundImage(new FillImage(PDUtil.getImage("img/semitrans4.png")));
		setTitleForeground(Color.DARKGRAY);
		setTitleHeight(new Extent(22));
		setTitleInsets(new Insets(8, 2, 0, 0));

		SplitPane verticalSplitPane = new SplitPane(SplitPane.ORIENTATION_VERTICAL_BOTTOM_TOP);
		verticalSplitPane.setSeparatorPosition(new Extent(18));
		add(verticalSplitPane);

		SplitPane rowFooter = new SplitPane();
		rowFooter.setSeparatorPosition(new Extent(48));
		verticalSplitPane.add(rowFooter);

		LabelEx lblAuthor = new LabelEx("Author:");
		lblAuthor.setInsets(new Insets(6, 0, 0, 0));
		lblAuthor.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, new Extent(10)));
		lblAuthor.setForeground(Color.DARKGRAY);
		rowFooter.add(lblAuthor);

		txtAuthor = new TextFieldEx();
		txtAuthor.setWidth(new Extent(92, Extent.PERCENT));
		txtAuthor.setHeight(new Extent(14));
		txtAuthor.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, new Extent(12)));
		txtAuthor.setForeground(Color.DARKGRAY);
		txtAuthor.setBackground(EccosDefaultLookAndFeel.HIGHLIGHT_COLOR);
		txtAuthor.setInsets(new Insets(3, 0, 0, 0));
		txtAuthor.setBorder(new Border(1, Color.DARKGRAY, Border.STYLE_DOTTED));
		rowFooter.add(txtAuthor);

		txtText = new TextAreaEx();
		txtText.setBackgroundImage(new FillImage(PDUtil.getImage("img/semitrans4.png")));
		txtText.setBorder(new Border(0, Color.WHITE, Border.STYLE_NONE));
		txtText.setWidth(new Extent(92, Extent.PERCENT));
		txtText.setHeight(new Extent(98, Extent.PERCENT));
		txtText.setInsets(new Insets(17, 0, 0, 0));
		txtText.setText(model.getText());
		verticalSplitPane.add(txtText);

		PDQuickleSlider sldCategory = new PDQuickleSlider("C");
		sldCategory.setInsets(new Insets(0, 12, 0, 0));
		sldCategory.addGridElement("Main Category", new SelectFieldEx(new String[] { "How-To", "Funny", "Server", "Organisation", "" }));
		sldCategory.addGridElement("Sub Category", new TextFieldEx(15));
		add(sldCategory);

		PDQuickleSlider sldShare = new PDQuickleSlider("S");
		sldShare.setTop(new Extent(-45));
		sldShare.setInsets(new Insets(0, 12, 0, 0));
		sldShare.addGridElement("Share with", new SelectFieldEx(new String[] { "Nobody", "My buddies", "RL Team", "Company wide", "Everybody" }));
		add(sldShare);

	}

	// we need to override, as a work-around for adding the toolbar
	@Override
	public boolean isValidChild(Component component) {
		return true;
	}

	/**
	 * @see nextapp.echo2.app.Component#processInput(java.lang.String,
	 *      java.lang.Object)
	 */
	@Override
	public void processInput(String inputName, Object inputValue) {
		if (INPUT_CLOSE.equals(inputName)) {
			userClose();
		} else if (PROPERTY_POSITION_X.equals(inputName)) {
			setPositionX((Extent) inputValue);
		} else if (PROPERTY_POSITION_Y.equals(inputName)) {
			setPositionY((Extent) inputValue);
		} else if (PROPERTY_WIDTH.equals(inputName)) {
			setWidth((Extent) inputValue);
		} else if (PROPERTY_HEIGHT.equals(inputName)) {
			setHeight((Extent) inputValue);
		} else if (Z_INDEX_CHANGED_PROPERTY.equals(inputName)) {
			setZIndex(((Integer) inputValue).intValue());
		}
	}

	public void update() {
		// Empty: Callback used to update a form when other
		// form is performing any related change
	}

	public void updateModel() {
		if (getPositionX() != null)
			model.setPositionX(getPositionX().getValue());
		if (getPositionY() != null)
			model.setPositionY(getPositionY().getValue());
		if (getWidth() != null)
			model.setWidth(getWidth().getValue());
		if (getHeight() != null)
			model.setHeight(getHeight().getValue());
		model.setText(txtText.getText());
		model.setAuthor(txtAuthor.getText());
	}

	public MQuickle getModel() {
		return model;
	}

	public void loadingFinished() {
		loading = false;
	}
}
